//
//  CommoneTools.m
//  dzm
//
//  Created by dzmmac on 14-9-26.
//  Copyright (c) 2014年 dzmmac. All rights reserved.
//

#import "CommoneTools.h"

#import "Reachability.h"
#import <UIKit/UIKit.h>


@implementation CommoneTools

+ (CommoneTools *) sharedManager{
    static CommoneTools *sharedManagerInstance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        sharedManagerInstance = [[self alloc] init];
    });
    return sharedManagerInstance;
}

+(BOOL) isConnectionAvailable{
    BOOL isExistenceNetwork = YES;
    Reachability *reach = [Reachability reachabilityWithHostName:@"www.baidu.com"];
    switch ([reach currentReachabilityStatus]) {
        case NotReachable:
            isExistenceNetwork = NO;
            break;
        case ReachableViaWiFi:
            isExistenceNetwork = YES;
            break;
        case ReachableViaWWAN:
            isExistenceNetwork = YES;
            break;
        default:
            isExistenceNetwork=YES;
            break;
    }
    return isExistenceNetwork;
}

+(NSString *)getCurrentNetwork{
    Reachability *reach = [Reachability reachabilityWithHostName:@"http://www.palm-portal.com/"];
    switch ([reach currentReachabilityStatus]) {
        case NotReachable:
            return @"None";
            break;
        case ReachableViaWWAN:
            return @"3G";
            break;
        case ReachableViaWiFi:
            return @"Wifi";
            break;
        case ReachableVia2G:
            return @"2G";
            break;
        case ReachableVia3G:
            return @"3G";
            break;
        case ReachableVia4G:
            return @"4G";
            break;
    }
}

/**
 *  改变视图的约束
 *
 *  @param view           需要改变的视图
 *  @param constraintHeight 约束的尺寸
 */
+(void)changeConstraintView:(UIView *)view constraintHeight:(float)constraintHeight
{
    NSArray* constrains = view.constraints;
    for (NSLayoutConstraint* constraint in constrains) {
        if (constraint.firstAttribute == NSLayoutAttributeHeight) {
            constraint.constant = constraintHeight;
        }
    }
}

/**
 *  改变视图的约束
 *
 *  @param view           需要改变的视图
 *  @param constraintHeight 约束的尺寸
 */
+(void)changeConstraintView:(UIView *)view constraintWidth:(float)constraintWidth
{
    NSArray* constrains = view.constraints;
    for (NSLayoutConstraint* constraint in constrains) {
        if (constraint.firstAttribute == NSLayoutAttributeWidth) {
            constraint.constant = constraintWidth;
        }
    }
}
@end
